package com.lxc.config;
import com.lxc.mapper.UserFollowsMapper;
import com.lxc.service.NotificationService;
import com.lxc.service.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class NovelUpdateConsumer {

    @Autowired
    private UserFollowsMapper userFollowsMapper;

    @Autowired
    private NotificationService notificationService;

    @RabbitListener(queues = RabbitMQConfig.NOVEL_UPDATED_QUEUE)
    public void handleNovelUpdate(byte[] message) {
        String workIdStr = new String(message);
        Integer workId = Integer.parseInt(workIdStr);

        // 查询所有关注该小说的用户 ID
        List<Integer> userIds = userFollowsMapper.getSubscribersByWorkId(workId);

        // 向每个用户发送通知
        for (Integer userId : userIds) {
            notificationService.sendNotification(userId, "您关注的小说有新章节更新：" + workId);
        }
    }
}

